/**
 * Created by dew on 2016/5/9.
 */
$('.dropimg').hide();
$('.droptxt').mouseenter(function () {
    $('.dropimg').show();
});
$('.dropimg').mouseenter(function () {
    $(this).show();
}).mouseleave(function () {
    $(this).hide();
})
function getLocalTime(nS) {
    return new Date(parseInt(nS) * 1000).toLocaleString().replace(/年|月/g, "-").replace(/日/g, " ");
}

//随机数
function  mathstr(){
    var rand = "";
    for(var i = 0; i < 13; i++){
        var r = Math.floor(Math.random() * 10);
        rand += r;
    }
    return rand;
}

//去空格
String.prototype.NoSpace = function()
{
    return this.replace(/\s+/g, "");
}




$(function () {
    //默认图片
    $('img').error(function () {
        $(this).attr('src', "/Public/Static/images/logos.jpg");
    });

    //ajax get请求
    $('.ajax-get').click(function () {
        var target;
        var that = this;
        if ($(this).hasClass('confirm')) {
            if (!confirm('确认要执行该操作吗?')) {
                return false;
            }
        }

        if ($(this).hasClass('confirms')) {
            if (!confirm('确认要执行该操作吗?提交后将不能再次更改!')) {
                return false;
            }
        }

        if ((target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            $.get(target).success(function (data) {
                if (data.status == 1) {
                    if (data.url) {
                        updateAlert(data.info + ' 页面即将自动跳转~', 'alert-success');
                    } else {
                        updateAlert(data.info, 'alert-success');
                    }
                    setTimeout(function () {
                        if (data.url) {
                            location.href = data.url;
                        } else if ($(that).hasClass('no-refresh')) {
                            $('#top-alert').find('button').click();
                        } else {
                            location.reload();
                        }
                    }, 1500);
                } else {
                    updateAlert(data.info);
                    setTimeout(function () {
                        if (data.url) {
                            location.href = data.url;
                        } else {
                            $('#top-alert').find('button').click();
                        }
                    }, 1500);
                }
            });

        }
        return false;
    });


    //ajax post submit请求
    $('.ajax-post').click(function () {

        var target, query, form;
        var target_form = $(this).attr('target-form');
        var that = this;
        var nead_confirm = false;

        if (($(this).attr('type') == 'submit') ||($(this).attr('type') == 'button') || (target = $(this).attr('href')) || (target = $(this).attr('url'))) {
            form = $('.' + target_form);
            if ($(this).attr('hide-data') === 'true') {//无数据时也可以使用的功能

                form = $('.hide-data');
                query = form.serialize();

            } else if (form.get(0) == undefined) {
                return false;
            } else if (form.get(0).nodeName == 'FORM') {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                if ($(this).attr('url') !== undefined) {
                    target = $(this).attr('url');
                } else {
                    target = form.get(0).action;
                }
                query = form.serialize();
            } else if (form.get(0).nodeName == 'INPUT' || form.get(0).nodeName == 'SELECT' || form.get(0).nodeName == 'TEXTAREA') {
                form.each(function (k, v) {
                    if (v.type == 'checkbox' && v.checked == true) {
                        nead_confirm = true;
                    }
                })
                if (nead_confirm && $(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.serialize();
            } else {
                if ($(this).hasClass('confirm')) {
                    if (!confirm('确认要执行该操作吗?')) {
                        return false;
                    }
                }
                query = form.find('input,select,textarea').serialize();
            }
            $(that).addClass('disabled').attr('autocomplete', 'off').prop('disabled', true);

            $.ajax({
                url: target,
                type: 'post',
                data: query,
                dataType: 'json',
                success: function (data) {
                    //console.log(data);


                    if (data.status == 1) {
                        if (data.url) {
                            updateAlert(data.info + ' 页面即将自动跳转~', 'alert-success');
                        } else {
                            updateAlert(data.info, 'alert-success');
                        }
                        setTimeout(function () {
                            if (data.url) {
                                location.href = data.url;
                            } else if ($(that).hasClass('no-refresh')) {
                                $('#top-alert').find('button').click();
                                $(that).removeClass('disabled').prop('disabled', false);
                            } else {
                                location.reload();
                            }
                        }, 1500);
                    } else {
                        updateAlert(data.info);
                        setTimeout(function () {
                            if (data.url) {
                                location.href = data.url;
                            } else {
                                $('#top-alert').find('button').click();
                                $(that).removeClass('disabled').prop('disabled', false);
                            }
                        }, 1500);
                    }
                }
            })

        }
        return false;
    });

    /**顶部警告栏*/
    var content = $('#main');
    var top_alert = $('#top-alert');
    top_alert.find('.close').on('click', function () {
        top_alert.removeClass('block').slideUp(200);

    });
    window.updateAlert = function (text, c) {

        text = text || 'default';
        c = c || false;
        if (text != 'default') {

            top_alert.find('.alert-content').text(text);

            if (top_alert.hasClass('block')) {

            } else {

                top_alert.addClass('block').slideDown(200);
                //content.animate({paddingTop:'+=55'},200);
                //alert()

            }
        } else {

            if (top_alert.hasClass('block')) {

                top_alert.removeClass('block').slideUp(200);
                // content.animate({paddingTop:'-=55'},200);
            }
        }
        //alert();
        if (c != false) {

            top_alert.removeClass('alert-danger alert-warning alert-info alert-success').addClass(c);
        }
    };
    //文件上传
    $(".uploads").change(function () {
        alert();
    })


});

//倒计时
function downTime() {
    var i = $(".stime").html();
    i--;
    $(".stime").html(i);
    if (i == 0) {
        $(".send-email").css('display', 'block');
        $(".daoji").css('display', 'none');
        $(".stime").html(120);
    }

    if (i > 0) {
        setTimeout(downTime, 1000)
    }
}









